package in.muton.edushare.components.forms.univsettings;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.api.Authentication;
import in.muton.edushare.wicket.converters.choicerenderers.GenericRenderer;

import java.util.HashMap;

import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

import com.edushare.common.vo.Universities;

public class UnivAcad extends Panel {

	public UnivAcad(String id, IModel<Universities> aModel) {
		super(id, aModel);
		model = aModel;
		auth = new Authentication(model.getObject(), EduShareSession.get()
				.getLoggedInUser());
		HashMap<String, String> hm = new HashMap<String, String>();
		hm.put("University Administrators", Authentication.ADMINS);
		hm.put("University and Department Administrators",
				Authentication.DEPT_MAIN_ADMINS);
		hm.put("Administrators and Teachers", Authentication.TEACHERS);

		final GenericRenderer<String> gr1 = new GenericRenderer<String>(hm,
				auth.getSetting(Authentication.PROG_NEW));
		final GenericRenderer<String> gr2 = new GenericRenderer<String>(hm,
				auth.getSetting(Authentication.COURSE_NEW));
		final GenericRenderer<String> gr3 = new GenericRenderer<String>(hm,
				auth.getSetting(Authentication.SEM_NEW));

		Form<Void> f = new Form<Void>("acadForm") {
			private static final long serialVersionUID = 3319283623093277400L;

			@Override
			public void onSubmit() {				
				auth.changeSetting(Authentication.PROG_NEW, gr1.getSelection());
				auth.changeSetting(Authentication.COURSE_NEW, gr2.getSelection());
				auth.changeSetting(Authentication.SEM_NEW, gr3.getSelection());				
				model.getObject().setSettings(auth.getString());
				EduShareSession.get().getSessionORMInterface().commitAllChanges();
			}
		};
		add(f);
		f.add(new DropDownChoice<String>("newProg", gr1.getModel(), gr1
				.getOptions(), gr1));
		f.add(new DropDownChoice<String>("newCourse", gr2.getModel(), gr2
				.getOptions(), gr2));
		f.add(new DropDownChoice<String>("semSwap", gr3.getModel(), gr3
				.getOptions(), gr3));

	}

	private IModel<Universities> model;
	Authentication auth = null;
	private static final long serialVersionUID = -4286931286054681790L;
}
